#include "config.h"
#include "GPIO_InIt.h"
#include "IR_Exit.h"

unsigned char Address;
unsigned char Command;
unsigned char NUM;

unsigned char isLightOn = 0;

void UART_config(void)
{

	COMx_InitDefine		COMx_InitStructure;					
	COMx_InitStructure.UART_Mode      = UART_8bit_BRTx;
	COMx_InitStructure.UART_BRT_Use   = BRT_Timer1;
	COMx_InitStructure.UART_BaudRate  = 9600;
	COMx_InitStructure.UART_RxEnable  = ENABLE;
	COMx_InitStructure.BaudRateDouble = DISABLE;
	COMx_InitStructure.UART_Interrupt = ENABLE;
	COMx_InitStructure.UART_Priority  = Priority_0;
	COMx_InitStructure.UART_P_SW      = UART1_SW_P30_P31;
	UART_Configuration(UART1, &COMx_InitStructure);
	EA=1;//中断总开关
//	TX1_write2buff(0xAA);	
}

void main()
{
	GPIO_Init();	
	UART_config();
	IR_Init();
	Timer1_Init();
	while(1)
	{			
		if(IR_GetDataFlag())//如果收到数据帧按键按下
		{
			Command=IR_GetCommand();//获取遥控器命令码			
			
			if(Command==0x0C)
			{
				Yin_Dao=1;
				Delay_ms(1);
				Yin_Dao=0;
			}
			else if(Command == 0x40)
			{
				isLightOn++;
				isLightOn = isLightOn+100;
//				if(isLightOn==1)
//				{
//					JDQ_SW=1;// 执行关灯操作
//					isLightOn = 0;
//				} 
//				else
//				{
//					JDQ_SW=0;// 执行开灯操作
//					isLightOn = 1;
//				}
			}
			TX1_write2buff(isLightOn);
		}
			
		if(IR_GetRepeatFlag())//收到连发信号表示按键一直按住
		{
			if (Command == 0x18)
			{
				MA=1;
				MB=0;
            } 
			else if (Command == 0x5E)
			{
				MA=0;
				MB=1;
            }	
		}
	}	
}

void Timer1_ISR() interrupt 3//100ms
{
	static unsigned int T1Count1;
	T1Count1++;	
	if(T1Count1>=5)
	{
		T1Count1=0;
		if(!IR_GetRepeatFlag())
		{
			MA = 0;
			MB = 0;
		}
	}
}
